Android LinearLayout动态增加item时Weight的设置

Android LinearLayout动态增加item时Weight的设置即可。

  • child item可以从xml中直接inflate出来。

  • 这里有个问题是,动态增加的话,child item中的weight,在add到LinearLayout后无效(用LinearLayout,很多时候就是冲着weight去的,因为可以很容易做到对齐和分割)。

  • 唯一的办法是在addView的时候用新的LayoutParams来指定weight:

addView

1
addView(child, new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1));
  • Android的layout配置中,类似的坑数不尽。。。再加上不同OS版本的不同表现,简直了T_T。。
    1
    setWeightSum(childCount);